import base64
import hashlib
import json
import random

import requests

# 钉钉机器人调用函数
# 钉钉机器人设置
headers = {'Content-Type': 'application/json'}  # 定义数据类型
webhooks1 = [
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1079a5bf-272a-4358-ab46-7766f9b4078a',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9df24480-d4d1-402b-8755-6484a8181a3b',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8d5c4cd8-7154-4c12-917c-14391bedea1a',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e6ae3708-841d-4c14-aa90-7e38e18e69a2'
]
webhooks2 = [
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=915e274d-d51c-4430-a32a-c2a84c53ac33',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0be62b36-e727-4d35-af86-7eb34caa3b03',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=606326e7-6c81-4c78-b1e1-77b2cc15066e',
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=538086cc-b56c-4a23-9af4-5c50d5e93c49'

]
pass_word = "行情提示:"


def call_dingtalk(message, type, test=True, picAddress = None,picurl = None):
    if test == False:
        webhook = random.choice(webhooks1)
    else:
        webhook = random.choice(webhooks2)
    data = None
    if type == 'text':
        # 钉钉test
        data = {
            "msgtype": "text",
            "text": {
                "content": pass_word + "\n" + message
            },
            "at": {
                "atMobiles": [
                    "16620498667",
                ],
                "isAtAll": False
            }
        }
    elif type == "markdown":
        text = ''
        for i in range(len(message)):
            text = text + message[i] + "\n"

        data = {
            "msgtype": 'markdown',
            "markdown": {
                "content": text
            },
            "at": {
                "atMobiles": [
                    "16620498667",
                ],
                "isAtAll": False
            }
        }
    elif type == "image":
        # 读取图片文件
        with open(picAddress, "rb") as f:
            fd = f.read()
            base64Content = str(base64.b64encode(fd), "utf-8")
        with open(picAddress, "rb") as f:
            fd = f.read()
            md = hashlib.md5()
            md.update(fd)
            md5Content = md.hexdigest()

        data = {
            "msgtype": "image",
            "image": {
                "base64": base64Content,
                "md5": md5Content
            }
        }
    elif type == "news":
        data = {
            "msgtype": "news",
            "news": {
                "articles": [
                    {
                        "title": message[0],
                        "description": message[1],
                        "url": picurl,
                        "picurl": picurl
                    }
                ]
            }
        }
    try:
        proxy = {
            'http': 'http://127.0.0.1:7890',
            'https': 'http://127.0.0.1:7890',
        }
        requests.post(webhook, data=json.dumps(data), headers=headers,proxies=proxy)  # 发送post请求
    except:
        print("钉钉机器人出现错误!")
        return None


if __name__ == '__main__':
    # call_dingtalk("this is a ding ding test", 'text')
    # call_dingtalk(["title", "this is a ding ding test"], 'markdown')
    # call_dingtalk("this is a ding ding test", 'image', picAddress="../Indicators/bolltest.png")

    # 测试新闻
    message = []
    message.append("行情提示")
    message.append("东山精密出现买入信号")
    call_dingtalk(message, 'news', picurl="https://s2.loli.net/2024/12/01/dwaiJVLhcTDnvo9.png")


